#import <GraphicsServices/GraphicsServices.h>
#import "MyDrawView.h"
#import "TouchApp.h" // adhoc

@implementation MyDrawView

/*
static float color1[4] = {1, 1, 1, 1};
static float color2[4] = {0, 0, 0, 1};
static char bgstring[] = "please touch me!";
*/

- (id)initWithFrame:(CGRect)frame {
  [super initWithFrame:frame];

  [super setTapDelegate: self];

  rect1 = CGRectMake(50, 150, 64, 64);
  rect2 = CGRectMake(200, 150, 64, 64);

  image1 = [UIImage applicationImageNamed:@"image1.png"];
  image2 = [UIImage applicationImageNamed:@"image2.png"];

  [self setBgcolor:0:0:0:1];

  return self;
}

- (void)dealloc {
	[image1 release];
	[image2 release];
	[super dealloc];
}

- (void)setSuperApp:(id)app {
  _app = app;
}

- (void)setBgcolor:(float)r:(float)g:(float)b:(float)a {
  bgcolor[0] = r;
  bgcolor[1] = g;
  bgcolor[2] = b;
  bgcolor[3] = a;
}

- (void)drawRect:(CGRect)rect {
  CGContextRef context = UICurrentContext(); 

  CGContextSetFillColor(context, bgcolor); 
  CGContextFillRect(context, rect);
  
  /*
  CGContextSetStrokeColor(context, color1); 
  CGContextShowTextAtPoint(context, 50.0, 50.0, bgstring, 5);

  CGContextSetFillColor(context, color1); 
  CGContextFillRect(context, rect1);

  CGContextSetFillColor(context, color2); 
  CGContextFillRect(context, rect2);
  */

  [image1 draw1PartImageInRect: rect1];
  [image2 draw1PartImageInRect: rect2];
}

// mouse 

- (BOOL)ignoresMouseEvents {
    return NO;
}

- (void)mouseDown:(GSEvent *)event {
  mousePoint = GSEventGetLocationInWindow(event);

  [(TouchApp *)_app appendStatus1:[NSString stringWithFormat:@"mouseDown:    %f, %f", 
                                mousePoint.x, mousePoint.y]];
  [self handleRect:mousePoint];
}
  
- (void)mouseDragged:(GSEvent *)event {
  mousePoint = GSEventGetLocationInWindow(event);

  [(TouchApp *)_app appendStatus1:[NSString stringWithFormat:@"mouseDragged: %f, %f", 
                                mousePoint.x, mousePoint.y]];
  [self handleRect:mousePoint];
}

- (void)mouseUp:(GSEvent *)event {
  mousePoint = GSEventGetLocationInWindow(event);

  [(TouchApp *)_app appendStatus1:[NSString stringWithFormat:@"mouseUp:      %f, %f", 
                                mousePoint.x, mousePoint.y]];
  [self handleRect:mousePoint];
}

- (void)mouseEntered:(GSEvent *)event {
  mousePoint = GSEventGetLocationInWindow(event);

  [(TouchApp *)_app appendStatus1:[NSString stringWithFormat:@"mouseEntered: %f, %f", 
                                mousePoint.x, mousePoint.y]];
}

- (void)mouseExited:(GSEvent *)event {
  mousePoint = GSEventGetLocationInWindow(event);

  [(TouchApp *)_app appendStatus1:[NSString stringWithFormat:@"mouseExited:  %f, %f", 
                                mousePoint.x, mousePoint.y]];
}

- (void)mouseMoved:(GSEvent *)event {
  mousePoint = GSEventGetLocationInWindow(event);

  [(TouchApp *)_app appendStatus1:[NSString stringWithFormat:@"mouseMoved:   %f, %f", 
                                mousePoint.x, mousePoint.y]];
}

// gesture

- (BOOL)canHandleGestures {
   return YES;
}

- (void)gestureStarted:(GSEvent *)event {
  left  = GSEventGetInnerMostPathPosition(event);
  right = GSEventGetOuterMostPathPosition(event);

  [(TouchApp *)_app appendStatus2:[NSString stringWithFormat:@"gestureStarted left: %3.0f, %3.0f right: %3.0f, %3.0f", left.x, left.y, right.x, right.y]];

  [self handleRect:left];
  [self handleRect:right];
}

- (void)gestureEnded:(GSEvent *)event {
  left  = GSEventGetInnerMostPathPosition(event);
  right = GSEventGetOuterMostPathPosition(event);

  [(TouchApp *)_app appendStatus2:[NSString stringWithFormat:@"gestureEnded   left: %3.0f, %3.0f right: %3.0f, %3.0f", left.x, left.y, right.x, right.y]];
  [self handleRect:left];
  [self handleRect:right];
}

- (void)gestureChanged:(GSEvent *)event {
  left  = GSEventGetInnerMostPathPosition(event);
  right = GSEventGetOuterMostPathPosition(event);

  [(TouchApp *)_app appendStatus2:[NSString stringWithFormat:@"gestureChanged left: %3.0f, %3.0f right: %3.0f, %3.0f", left.x, left.y, right.x, right.y]];
  [self handleRect:left];
  [self handleRect:right];
}

// tap

- (void)view:(UIView *)view handleTapWithCount:(int)count event:(GSEvent *)event {
  CGPoint point = GSEventGetLocationInWindow(event);

  if (count == 2) {
    [(TouchApp *)_app toggleLogMode];
  }
  [(TouchApp *)_app appendStatus2:[NSString stringWithFormat:@"tap: %d at %3.0f, %3.0f", 
                                count, point.x, point.y]];

}

- (void)view:(UIView *)view handleTapWithCount:(int)count event:(GSEvent *)event fingerCount:(int)finger {
  CGPoint point = GSEventGetLocationInWindow(event);

  if (count == 2) {
    [(TouchApp *)_app toggleLogMode];
  }
  [(TouchApp *)_app appendStatus2:[NSString stringWithFormat:@"tap: %d with %d finger at %3.0f, %3.0f", 
                                count, finger, point.x, point.y]];

}

// swipe

- (BOOL)canHandleSwipes {
  return YES;
}

- (int)swipe:(int)swipeDirection withEvent:(GSEvent *)event { 
  CGPoint point = GSEventGetLocationInWindow(event);

  [(TouchApp *)_app appendStatus2:[NSString stringWithFormat:@"swipe: %d at %3.0f, %3.0f", 
                                swipeDirection, point.x, point.y]];
} 

// handle rects

- (BOOL)handleRect:(CGPoint)point {
  if (CGRectContainsPoint(rect1, point)) {
    rect1 = CGRectMake(point.x - (rect1.size.width / 2),
                       point.y - (rect1.size.height / 2),
                       rect1.size.width, rect1.size.height);
    [self setNeedsDisplay];
    return YES;
  }
  if (CGRectContainsPoint(rect2, point)) {
    rect2 = CGRectMake(point.x - (rect2.size.width / 2),
                       point.y - (rect2.size.height / 2),
                       rect2.size.width, rect2.size.height);
    [self setNeedsDisplay];
    return YES;
  }
  return NO;
}

- (void)swapImage {
  UIImage *tmp;
  tmp = image1;
  image1 = image2;
  image2 = tmp;
}

@end
